<?php
import('@.Common.WhereMachine');
class EventWhereMachine extends WhereMachine{
    public function role($role){
        if (is_null($role)){
            return;
        }
        if (0===strpos('group', $role)){
            $role= AccountModel::C('ROLE_GROUP');
        } else if(0===strpos('user', $role)){
            $role= AccountModel::C('ROLE_USER_BASIC');
        }
        $this->map['role']= $role;
    }
    public function accountRole($role){
        if (is_null($role)){
            return $this;
        }
        return $this->set('Account.role', $role);
    }
    public function type($type){
        return $this->set('type', array('IN', $type));
    }
    public function author($uid){
        return $this->set('author_id', $uid);
    }
    public function state($state){
        if (is_null($state)){
            return;
        }
        $this->map['visibility']= $state;
    }
    public function date($year, $month=null){
        if (!$year){
            return;
        }
        if (!$month || $month<1 || $month>12){
            $month= null;
            $t= date('Y-m-d',mktime(0, 0, 0, 1, 1, $year+1));
        } else{
            $t= date('Y-m-d',mktime(0, 0, 0, $month+1, 1, $year));
        }
        $this->map['time_start']= array('elt', $t);
    }
    public function authorState($state){
        if (is_null($authorState)){
            return;
        }
        $this->map['Account.state']= $state;
    }
    public function time($time){
        if (is_null($time)){
            return;
        }
        return $this->set('Event.time',array('ELT', $time));
    }
    /****
    * @v 活动是否已经结束
    */
    public function isEnded($v){
        if ($v){
            $cond= 'LT';    //小于当前日期,已经结束
        } else{
            $cond= 'EGT';
        }
        //TODO 历史原因, 将null值算上
        return $this->set('time_end', array($cond, date(DATE_ATOM)));
    }
    /****
    * 搜索时间段内的活动, 推荐搜索完成之后按活动开始时间排序
    * @timeMin 开始时间, array(year, mon[, mday=1]) 将按当天00:00计算
    * @timeMax 结束时间, array(year, mon[, mday=32]) 将按当天24:00计算
    * 由于历史原因, 没有明确开始\结束时间的活动将被直接排除
    */
    public function timeRange($timeMin, $timeMax){
        if (!is_null($timeMin)){
            if (!array_key_exists('mday', $timeMin)){
                $timeMin['mday']= 1;
            }
            $timeMin['hours']= 0;
            $s= date(DATE_ATOM, mktime($timeMin['hours'], 0, 0, $timeMin['mon'], $timeMin['mday'], $timeMin['year']));
            $this->set('time_start', array('LT', $t));  //开始时间在区间结束前
        }
        if (!is_null($timeMax)){
            if (!array_key_exists('mday', $timeMax)){
                $timeMax['mon']++;
                $timeMax['mday']= 0;
            }
            $timeMax['hours']= 24;
            $t= date(DATE_ATOM, mktime($timeMax['hours'], 0, 0, $timeMax['mon'], $timeMax['mday'], $timeMax['year']));
            $this->set('time_end', array('gt', $s));    //结束时间在区间开始后
        }
        
        return $this;
    }
    public function word($q){
        if (is_null($q) || strlen($q)<=0){
            return $this;
        }
    //TODO: 文字检索
        return $this->set('title', array('LIKE', '%'.$q.'%'));
    }
    /****
    * @tag 可以为数组
    */
    public function tag($tag){
        //TODO
        if (is_null($tag)){
            return $this;
        }
        $etm= M('EventTag');
        $ids= $etm->where(array('tag'=>array('IN', $tag)))->field('eid')->select();
        if (is_null($ids)){
            $ids=array();   //这里不能跳过搜索, 而应返回空结果
        }
        foreach ($ids as &$v){
            $v= $v['eid'];
        }
        return $this->set('id', array('IN', $ids));
    }
}
?>